/*
 * Copyright 2016-2021 Pnoker. All Rights Reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xj.business.airc.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ie.api.center.data.fegin.RealDataClient;
import com.ie.api.center.manager.feign.DeviceClient;
import com.ie.api.center.manager.feign.DriverClient;
import com.ie.api.center.manager.feign.PointClient;
import com.ie.api.center.manager.feign.TenantClient;
import com.xj.business.airc.bean.model.ModelContent;
import com.xj.business.airc.bean.model.ModelData;
import com.xj.business.airc.service.PHomepageService;
import com.xj.business.airc.service.job.ModelScheduleJob;
import com.xj.common.bean.R;
import com.xj.common.bean.point.PointValue;
import com.xj.common.model.Device;
import com.xj.common.model.Driver;
import com.xj.common.model.Point;
import com.xj.common.model.Tenant;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 模型数据初始化，这里设置定时器进行模型固定时间的更新
 */
@Slf4j
@Service
public class PHomePageServiceImpl implements PHomepageService {

    @Resource
    private Scheduler scheduler;

    @Resource
    private TenantClient tenantClient;

    @Resource
    private DriverClient driverClient;


    @Resource
    private DeviceClient deviceClient;

    @Resource
    private PointClient pointClient;

    @Resource
    private ModelContent modelContent;

    @Resource
    private RealDataClient realDataClient;

    @Override
    public Map<String ,String > GetAllByTenantid(Long tenantId, Boolean history) {

        Float PAll = 0f;
        Float KWAll = 0f;

        List<Device> devices = new ArrayList<>();
        for (Driver driver: modelContent.getModelData().getDrivers().get(tenantId)){
            devices.addAll(modelContent.getModelData().getDevices().get(driver.getId()));
        }
        for(Device device : devices){

            for(Point point : modelContent.getModelData().getPoints().get(device.getId())){
                if(point.getName().equals("P")){
                   PAll+=Float.parseFloat(realDataClient.handleGetRealDataByDeviceIdAndPointID(
                           getJo(device.getId(), point.getId(),history))
                           .getData().getRawValue());
                }else if(point.getName().equals("kW")){
                    KWAll+=Float.parseFloat(realDataClient.handleGetRealDataByDeviceIdAndPointID(
                            getJo(device.getId(), point.getId(),history))
                            .getData().getRawValue());
                }

            }
        }
        Map<String ,String > result = new HashMap<>();
        result.put("P",PAll.toString());
        result.put("kW",KWAll.toString());
        result.put("NUM",devices.size()+"/"+devices.size());

        return result;

    }

    @Override
    public List<Map<String,Object>> GetAllDeviceByTenantid(Long tenantId) {

        List<Device> devices = new ArrayList<>();
        for (Driver driver: modelContent.getModelData().getDrivers().get(tenantId)){
            devices.addAll(modelContent.getModelData().getDevices().get(driver.getId()));
        }
        List<Map<String,Object>> resultList= new ArrayList<>();
        for(Device device:devices){
            Point p = modelContent.getModelData().getPNameToPoints().get(device.getId()).get("OPEN");
            //获取在线实时数据
            JSONObject jo = new JSONObject();
            jo.put("deviceId",device.getId());
            jo.put("pointId",p.getId());
            jo.put("history",false);
            R<PointValue> pointValueR= realDataClient.handleGetRealDataByDeviceIdAndPointID(jo);

            //封装处一个Device信息
            Map<String,Object> deviceMapRes =  new HashMap<>();
            deviceMapRes.put("id",device.getId());
            deviceMapRes.put("name",device.getName());
            deviceMapRes.put("sn",device.getSn());
            if(pointValueR.isOk())
                deviceMapRes.put("open",pointValueR.getData().getRawValue());
            else
                deviceMapRes.put("open",0);//表征设备信号中断

            resultList.add(deviceMapRes);
        }
        return resultList;
    }

    private JSONObject getJo(Long deviceId,Long pointId,Boolean history){
        JSONObject jo = new JSONObject();
        jo.put("deviceId",deviceId);
        jo.put("pointId",pointId);
        jo.put("history",history);
        return jo;
    }
}
